package com.kaluli.project.repository.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.kaluli.project.domain.entity.KllOrderDataVo;
import com.kaluli.project.domain.entity.KllUserDataVo;
import com.kaluli.project.domain.repository.KllDataRepository;
import com.kaluli.project.domain.repository.KllOrderRepository;
import com.kaluli.system.repository.AbstractRepositorySupport;

@Repository
public class KllOrderRepositoryImpl extends AbstractRepositorySupport<KllOrderDataVo> implements KllOrderRepository {

	@Override
	protected Class<KllOrderDataVo> getEntityClass() {
		// TODO Auto-generated method stub
		return KllOrderDataVo.class;
	}

	public List<KllOrderDataVo> findByWeek(String startTime) {
		StringBuffer sql = new StringBuffer(
				"SELECT o.id,o.order_number, o.depot_type,o.order_time,s.content,s.created_at as start_time,s.updated_at as end_time,TIMESTAMPDIFF(HOUR, o.order_time,s.created_at)/24 AS deliv_time, TIMESTAMPDIFF(HOUR, o.order_time,s.updated_at)/24 AS expr_time ");
		sql.append(" FROM `kll_order` o left join `kll_order_logistics` s ");
		sql.append(
				"  on o.order_number= s.order_number where (o.depot_type =1 OR o.depot_type= 10 OR o.depot_type=19 OR o.depot_type=23) ");
		sql.append(" and  o.`order_time`>'" + startTime + "' ");
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String today = sf.format(new Date());
		sql.append("  and o.`order_time` <'" + today + "' ");
		sql.append(" and s.domestic_status='shutdown' ");
		sql.append(" ORDER BY o.depot_type ");
		List<KllOrderDataVo> list = excuteSql(sql.toString());

		return list;
	}
	
	public List<KllOrderDataVo> findByData(String type) {
		StringBuffer sql = new StringBuffer(
				"SELECT o.id,o.order_number, o.depot_type,o.order_time,s.content,s.created_at as start_time,s.updated_at as end_time,TIMESTAMPDIFF(HOUR, o.order_time,s.created_at)/24 AS deliv_time, TIMESTAMPDIFF(HOUR, o.order_time,s.updated_at)/24 AS expr_time ");
		sql.append(" FROM `kll_order` o left join `kll_order_logistics` s ");
		sql.append(
				"  on o.order_number= s.order_number where o.depot_type= "+type+" ");
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sql.append(" and s.domestic_status='shutdown' ");
		sql.append(" ORDER BY o.`order_time` DESC LIMIT 100 ");
		List<KllOrderDataVo> list = excuteSql(sql.toString());

		return list;
	}

}
